//
//  oldFamilyFirstViewController.m
//  laonianbangfamily
//
//  Created by 勇拓 李 on 18/3/23.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldFamilyFirstViewController.h"
#import "oldFamilyFirstViewHeader.h"
#import "oldSearchViewController.h"
#import "oldServiceDetailInfoViewController.h"
#import "oldFamilyBandParentsViewController.h"
#import "oldMyParentsViewController.h"
#import "oldSuspendTopOrderView.h"
#import "oldWatingViewController.h"
#import "OrderInfoViewController.h"
#import "oldActivityFirstTableViewCell.h"
#import "oldActivityViewController.h"

@interface oldFamilyFirstViewController ()<oldFamilyFirstViewHeaderDelegate, oldSuspendTopOrderView>
@property (nonatomic, copy)NSArray *dataSource;
@property (nonatomic, strong)oldSuspendTopOrderView *topOrderView;
@end

static NSString *const identifier = @"oldActivityFirstTableViewCell";

@implementation oldFamilyFirstViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardWillShow:(NSNotification *)note
{
    [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
    
    oldSearchViewController *vc = [[oldSearchViewController alloc] initWithNibName:@"oldSearchViewController" bundle:nil];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:NO];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.topOrderView refreshData];
    [self refresh];
    self.topOrderView.hidden = NO;
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.topOrderView.hidden = YES;
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)needReload
{
    [self.tableView.mj_header beginRefreshing];
}

- (void)setCount:(NSInteger)count number:(NSInteger)number
{
    [self.topOrderView setCount:count number:number];
}

- (oldSuspendTopOrderView *)topOrderView
{
    if (!_topOrderView) {
        _topOrderView = [[[NSBundle mainBundle] loadNibNamed:@"oldSuspendTopOrderView"
                                                       owner:self
                                                     options:nil] objectAtIndex:0];;
        _topOrderView.delegate = self;
    }
    
    return _topOrderView;
}

#pragma mark - initView
- (void)initView
{
    [self rigisterCell:identifier];
    self.view.backgroundColor = [UIColor whiteColor];
    [self initHeader];
}

- (void)initHeader
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 416)];
    self.tableView.tableHeaderView = view;
    oldFamilyFirstViewHeader *header = [[[NSBundle mainBundle] loadNibNamed:@"oldFamilyFirstViewHeader" owner:self options:nil] objectAtIndex:0];
    header.frame = view.bounds;
    header.delegate = self;
    [view addSubview:header];
}

#pragma mark - initData
- (void)initData
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)refresh
{
    WeakSelf
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"pageNum", @"10", @"pageSize", nil];
    
    [OLDNetWork httpRequestWithPostPut:URL_GetCategoryList params:params success:^(id data) {
        [weakSelf initDataSource:data];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}

- (void)initDataSource:(id)data
{
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < [data count]; i++) {
        oldActivity *activity = [[oldActivity alloc] initWithDictionary:data[i]];
        [array addObject:activity];
    }
    
    _dataSource = array;
}

#pragma mark - headerdelegate
- (void)ServiceChoosed:(oldService *)service
{
    oldServiceDetailInfoViewController *vc = [oldServiceDetailInfoViewController new];
    vc.hidesBottomBarWhenPushed = YES;
    vc.service = service;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)bannerClicked:(NSString *)url Type:(Old_BannerUrltype)type
{
    
}

#pragma mark - topOrderView delegate
- (void)ifNeedRemove:(BOOL)remove
{
    if (remove) {
        [_topOrderView removeFromSuperview];
    }
    else
    {
        if (![_topOrderView superview]) {
            [_topOrderView addToWindow];
        }
    }
}

- (void)SuspendTopViewTouched:(oldOrderInfo *)orderInfo count:(NSInteger)count number:(NSInteger)number
{
    if (orderInfo) {
        OrderInfoViewController *vc = [OrderInfoViewController new];
        vc.orderInfo = orderInfo;
        vc.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        oldWatingViewController *vc = [oldWatingViewController new];
        vc.hidesBottomBarWhenPushed = YES;
        vc.number = number;
        vc.count = count;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)orderFailed:(NSString *)reason
{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"订单失败" message:reason preferredStyle: UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:action];
    
    
    [self presentViewController:alertController animated:YES completion:nil];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_dataSource count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    oldActivityFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldActivityFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.activity = _dataSource[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 198;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    oldActivityViewController *vc = [oldActivityViewController new];
    vc.activity = _dataSource[indexPath.row];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

@end
